package com.pointstar.dp.modules.project.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.pointstar.dp.modules.project.dao.InBoundMapper;
import com.pointstar.dp.modules.project.entity.InBoundEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pointstar.dp.common.entity.Page;
import com.pointstar.dp.common.entity.Query;
import com.pointstar.dp.common.entity.R;
import com.pointstar.dp.common.utils.CommonUtils;
import com.pointstar.dp.modules.project.entity.InBoundDetailsEntity;
import com.pointstar.dp.modules.project.dao.InBoundDetailsMapper;
import com.pointstar.dp.modules.project.service.InBoundDetailsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入库明细表
 * @author Korey<m13057531285@163.com>
 */
@Service("inBoundDetailsService")
public class InBoundDetailsServiceImpl implements InBoundDetailsService {

	private final InBoundDetailsMapper inBoundDetailsMapper;
	private final InBoundMapper inBoundMapper;

	@Autowired
	public InBoundDetailsServiceImpl(InBoundDetailsMapper inBoundDetailsMapper,InBoundMapper inBoundMapper) {
		this.inBoundDetailsMapper = inBoundDetailsMapper;
		this.inBoundMapper = inBoundMapper;
	}

	/**
     * 分页查询
     * @param params
     * @return
     */
	@Override
	public Page<InBoundDetailsEntity> listInBoundDetails(Map<String, Object> params) {
		Query query = new Query(params);
		Page<InBoundDetailsEntity> page = new Page<>(query);
		inBoundDetailsMapper.inListForPage(page, query);
		return page;
	}

	@Override
	public List<InBoundDetailsEntity> listInBoundDetailsNoPage(Map<String, Object> params) {
		Query query = new Query(params);
		return inBoundDetailsMapper.inList(query);
	}

	@Override
	public Page<InBoundDetailsEntity> listBoundDetails(Map<String, Object> params) {
		Query query = new Query(params);
		Page<InBoundDetailsEntity> page = new Page<>(query);
		inBoundDetailsMapper.listForPage(page, query);
		return page;
	}

	@Override
	public List<InBoundDetailsEntity> listBoundDetailsNoPage(Map<String, Object> params) {
		Query query = new Query(params);
		return inBoundDetailsMapper.list(query);
	}


	/**
     * 新增
     * @param inBoundDetails
     * @return
     */
	@Override
	public R saveInBoundDetails(InBoundDetailsEntity inBoundDetails) {
		int count = inBoundDetailsMapper.save(inBoundDetails);
		return CommonUtils.msg(count);
	}

    /**
     * 根据id查询
     * @param id
     * @return
     */
	@Override
	public R getInBoundDetailsById(Long id) {
		InBoundDetailsEntity inBoundDetails = inBoundDetailsMapper.getObjectById(id);
		return CommonUtils.msg(inBoundDetails);
	}

    /**
     * 修改
     * @param inBoundDetails
     * @return
     */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public R updateInBoundDetails(InBoundDetailsEntity inBoundDetails) {
		//获取总金额
		BigDecimal totalPrice = inBoundDetails.getUnitPrice().multiply(new BigDecimal(inBoundDetails.getQuantity()));
		inBoundDetails.setTotalPrice(totalPrice);
		//更新入库商品明细信息
		int count = inBoundDetailsMapper.initUpdate(inBoundDetails);

		if (count == 0) {
			return R.error("入库单中已有商品出库,无法删除!");
		}

		//更新主表中的总金额
		long detailId = (long)inBoundDetails.getId();
		Long[] id = new Long[]{detailId};
		int count2 = inBoundDetailsMapper.updateInBoundAmount(id);

		return CommonUtils.msg(id, count2);
	}

    /**
     * 删除
     * @param id
     * @return
     */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public R batchRemove(Long[] id) {
		int count = inBoundDetailsMapper.updateDeleteFlag(id);
		if (count == 0) {
			return R.error("入库单中已有商品出库,无法删除!");
		}

		//更新主表中的总金额
		int count2 = inBoundDetailsMapper.updateInBoundAmount(id);
		BigDecimal zero = new BigDecimal(0);

		for(Long detailsId:id){
			//判断如果主表中的总金额是0，更新主表的delete_flag
			InBoundEntity inBoundEntity= inBoundMapper.getObjectByDetailsId(detailsId);
			int i = zero.compareTo(inBoundEntity.getAmount());
			if(i==0){
				long inBoundId = (long)inBoundEntity.getId();
				Long[] inBoundIds = new Long[]{inBoundId};
				//更新入库主表的delete_flag
				int count3 = inBoundMapper.updateDeleteFlag(inBoundIds);
			}
		}

		return CommonUtils.msg(id, count2);

	}

}
